/*
https://www.terraform.io/language/settings/backends/http

terraform 的 backends http 流程

- init
GET

- plan
lock
get
unlock

- apply
lock
get
post
unlock

-destroy
lock
get
post
unlock
*/
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

var state []byte

func main() {
	http.HandleFunc("/", foo)

	log.Fatal(http.ListenAndServe(":18080", nil))
}

func foo(w http.ResponseWriter, r *http.Request) {
	fmt.Println(time.Now(), r.Method)

	body, err := ioutil.ReadAll(r.Body)
	fmt.Println("err:", err)
	fmt.Println(string(body))

	var x = string(body)
	_ = x

	if r.Method == "GET" {
		w.Write(state)
	} else if r.Method == "POST" {
		state = body
	}
}
